using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Geekeradmin.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Org.BouncyCastle.Crypto;
using Publicuse.Autofac;
using Publicuse.Entity;
using Publicuse.Entity.other;
using Publicuse.Interface;
using Publicuse.Util;
using Publicuse.WebCore.SwaggerExtend;
using Quartz.Util;
using SharpCompress.Common;
using SqlSugar;

namespace Geekeradmin.Controllers.other
{
  /// <summary>
  /// 测试控制器
  /// </summary>
  [AllowAnonymous]
  [Route("api/[controller]")]
  [ApiController]
  [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V2))]
  public class HomeController : Controller
  {
    private readonly ILogger<HomeController> logger;
    private readonly IMySqlSugarService mySqlSugar;
    private readonly IWebHostEnvironment webHost;
    private readonly IMySqlFreeSqlService mySqlFreeSql;

    /// <summary>
    ///
    /// </summary>
    /// <param name="autoface"></param>
    /// <param name="mySqlSugar"></param>
    /// <param name="logger"></param>
    /// <param name="webHost"></param>
    /// <param name="options"></param>
    public HomeController(
      IAutofaceInstance autoface,
      IMySqlSugarService mySqlSugar,
      IMySqlFreeSqlService mySqlFreeSql,
      ILogger<HomeController> logger,
      IWebHostEnvironment webHost,
      IOptions<DBConnectionOption> options
    )
    {
      this.logger = logger;
      this.mySqlSugar = mySqlSugar;
      this.mySqlFreeSql = mySqlFreeSql;
      this.webHost = webHost;
    }

    /// <summary>
    /// 主方法
    /// </summary>
    ///<remarks>
    ///描述：这是一个负责测试主方法的
    /// WebApi
    ///</remarks>
    /// <returns></returns>
    [HttpGet("Index")]
    [CustomResourceAsync]
    public async Task<JsonResult> Index()
    {
      return await Task.Factory.StartNew(() =>
      {
        CommonResult result = new CommonResult();
        result.data = "系统初始成功";
        return new JsonResult(result);
      });
    }

    [HttpGet("test")]
    public async Task<string> test()
    {
      return await Task.Factory.StartNew(() =>
      {
        var table = TableAtributeUtil.GetTable<Spconf>();
        var sql = $"select * from {table} where id in (@Id);";
        var ids = "13,14".Split(',').Select(o => o.GetIntValue()).ToArray();
        List<SugarParameter> parameters = new List<SugarParameter>();
        parameters.Add(new SugarParameter("@Id", ids));
        var list = this.mySqlSugar.QueryBySqlPara<Spconf>(
          sql,
          parameters,
          out var msg
        );
        var testtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        if (!string.IsNullOrEmpty(msg))
        {
          return testtime + "测试连接数据库失败：" + msg;
        }
        else
        {
          return testtime + "测试连接数据库成功："+JsonUtil.ToJson(list);
        }
      });
    }

    private static string getFileBase64(string fileName)
    {
      FileStream filestream = new FileStream(fileName, FileMode.Open);
      byte[] arr = new byte[filestream.Length];
      filestream.Read(arr, 0, (int)filestream.Length);
      string baser64 = Convert.ToBase64String(arr);
      filestream.Close();
      return baser64;
    }

    /// <summary>
    /// 账号密码AES加密
    /// </summary>
    /// <returns></returns>
    [HttpGet("aesmm")]
    public string aesmm()
    {
      string result = string.Empty;
      //var table = TableAtributeUtil.GetTable<User>();
      //var sql = $"select Id,uPwd,uCode from {table};";
      //var list = this.mySqlSugar.Query<User>(sql, null, out var msg);
      //List<string> errlist= new List<string>();
      //if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
      //{
      //  foreach (var item in list)
      //  {
      //    try
      //    {
      //      var jmpwd = EnDecryptionUtil.AesDecrypt(item.uPwd);
      //      var aespwd = AesEncryptionUtil.Encrypt(jmpwd);
      //      sql = $"update {table} set uPwd='{aespwd}' where Id={item.Id};";
      //      this.mySqlSugar.ExecuteSql(sql, null, out msg);
      //    }
      //    catch(Exception ex)
      //    {
      //      errlist.Add($"账号【{item.uCode}】修改密码错误：" + ex.Message+"\r\n");
      //    }
      //  }
      //}
      return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") ;
    }
  }

  public class testm
  {
    public string name { get; set; }

    public string age { get; set; }
    public string address { get; set; }
  }
}
